上一篇介紹了 create type,以及 typed table.
本篇介紹應用,這次就不做複合型態的了.就是直接建立type,建立typed table,
然後刪除屬性,新增屬性.
create type ty_gal as (
id int
, name text
, stuff text
);
create table gal1 of ty_gal;
create table gal2 of ty_gal;
insert into gal1 values
(1, '小島南', '測試用'),
(2, '初川南', '測試用');
insert into gal2 values
(3, '相沢南', '測試用'),
(4, '小宵虎南', '測試用');
alter type ty_gal
drop attribute stuff cascade;
alter type ty_gal
add attribute products text[] cascade;
commit;
update gal1
set products = array['SSIS-340', 'SSIS-315']
where id = 1;
update gal1
set products = array['BBAN-359', 'SHKD-987']
where id = 2;
update gal2
set products = array['IPX-819', 'IPX-801']
where id = 3;
update gal2
set products = array['SSIS-309', 'SSIS-281']
where id = 4;
select *
from gal1
union all
select *
from gal2
order by id;
id | name | products
----+----------+---------------------
1 | 小島南 | {SSIS-340,SSIS-315}
2 | 初川南 | {BBAN-359,SHKD-987}
3 | 相沢南 | {IPX-819,IPX-801}
4 | 小宵虎南 | {SSIS-309,SSIS-281}
(4 rows)
線上展示連結:
https://dbfiddle.uk/?rdbms=postgres_14&fiddle=56f53842156bf29508fa0e853e88538e
上面的簡單範例中,可以看到兩個typed table 都不需要去使用 alter table 來修改欄位,
我們只要直接alter type ... cascade 就可以了.
相信聰明的你,應該有想到這對於系統發展,構型的一致性,有一定的幫助了.